/*
 * SearchEngine.java
 *
 * Created on 28. Februar 2008, 15:26
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package de.blitzcoder.collide.engine;


import de.blitzcoder.collide.Interface;
import de.blitzcoder.collide.gui.SearchDialog;
import de.blitzcoder.collide.gui.textarea.CollIDETextArea;
import javax.swing.JOptionPane;
import java.awt.event.*;
import org.gjt.sp.jedit.textarea.Selection;


/**
 *
 * @author blitzcoder
 */
public class SearchEngine {
    
    private CollIDETextArea textArea;
    private int             lastpos = 0;
    private SearchDialog    dialog;
    
    /** Creates a new instance of SearchEngine */
    public SearchEngine(CollIDETextArea textArea) {
        
        this.textArea = textArea;
        
        // Create the dialog
        dialog = new SearchDialog();
        
        dialog.addSearchButtonListener( new SearchBtnListener() );
        dialog.addSearchAndReplaceButtonListener( new ReplaceAndSearchBtnListener() );
        dialog.addReplaceAllButtonListener( new ReplaceAllBtnListener() );
        
    }
    
    /**
     * Methods to hide/show the Dialog
     */
    public void show() {
        dialog.setVisible(true);
        dialog.setReplaceBtnEnable(false);
    }
    public void hide() {
        dialog.setVisible(false);
    }
    
    /*
     * Find the next
     */
    public void findNext() {
        
        int oldpos = lastpos;
        
        find();
        
        if (oldpos != lastpos) {
            //codeEditor.codeArea.selectText(lastpos,lastpos+dialog.getSearchText().length());
            textArea.setSelection(new Selection.Range(lastpos,lastpos+dialog.getSearchText().length()) {
            });
        }
        
        if (!dialog.keepWindow()) {
            hide();
        }
        
    }
    
    /*
     * The search method
     */
    private void find() {
        
        
        String searchString = dialog.getSearchText();
        if (!dialog.caseSensetive()) {
            searchString = searchString.toLowerCase();
        }
        
        String searchInText = textArea.getText();
        if (!dialog.caseSensetive()) {
            searchInText = searchInText.toLowerCase();
        }
        
        
        //Find the next
        int newPos=-1;
        
        if (dialog.backwards()) {
            newPos = searchInText.lastIndexOf(searchString,lastpos-1);
        } else {
            newPos = searchInText.indexOf(searchString,lastpos+1);
        }
        
        if (newPos == -1) {
            
            JOptionPane.showMessageDialog(Interface.get(),"Seitenende erreicht!");
            
            if (dialog.restartOnTop()) {
                
                if (dialog.backwards()) {
                    lastpos = searchInText.length();
                } else {
                    lastpos = 0;
                }
                
                if (dialog.backwards()) {
                    newPos = searchInText.lastIndexOf(searchString,lastpos);
                } else {
                    newPos = searchInText.indexOf(searchString,lastpos);
                }
                
                if (newPos == -1) {
                    JOptionPane.showMessageDialog(Interface.get(),"'"+searchString+"' konnte nicht gefunden werden!");
                } else {
                    lastpos = newPos;
                }
            }
            
        } else {
            
            lastpos = newPos;
            
        }
        
    }
    
    private void closeWindow() {
        if (!dialog.keepWindow()) {
            hide();
        }
    }
    
    /*
     * Replace and search method
     */
    private void replaceAndFindNext() {
        
        textArea.getBuffer().remove(lastpos, dialog.getSearchText().length());
        textArea.getBuffer().insert(lastpos, dialog.getReplaceText());
        
        findNext();
        
    }
    
    // Get the String
    
    /*
     * Replace all method
     */
    private void replaceAll() {
        try {
            
            
            String text = textArea.getText();
            int pos=0;
            while (true) {

                int newpos = text.indexOf(dialog.getSearchText(),pos);

                if (newpos == -1)
                    break;

                textArea.getBuffer().remove(newpos, dialog.getSearchText().length());
                textArea.getBuffer().insert(newpos, dialog.getReplaceText());
                
                pos = newpos + 1;

            }
            
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    
    
    /**
     * The listener for the Search Button
     */
    private class SearchBtnListener implements ActionListener {
        public void actionPerformed(ActionEvent ev) {
            findNext();
            dialog.setReplaceBtnEnable(true);
        }
    }
    /**
     * The listener for the replace&search Button
     */
    private class ReplaceAndSearchBtnListener implements ActionListener {
        public void actionPerformed(ActionEvent ev) {
            replaceAndFindNext();
        }
    }
    /**
     * Listener for the replaceAll Button
     */
    private class ReplaceAllBtnListener implements ActionListener {
        public void actionPerformed(ActionEvent ev) {
            replaceAll();
        }
    }
    
    
}
